#!/usr/bin/python
# --coding:utf-8--


import requests

params = {'wd': 'python'}
headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
           }

"""
1、params 接收一个字典或者字符串的查询参数，字典类型自动转换为url编码，不需要使用urlencode()
2、"http://www.baidu.com/s中的 s 相当于调用搜索模块
3、headers为设置的请求头，User-Agent则是浏览器的用户代理字符串
"""
response = requests.get("http://www.baidu.com/s", params=params, headers=headers)

# 查看响应内容，response.text 返回的是Unicode格式的数据
print(type(response.text))
print(response.text)
# 查看响应内容，response.content返回的字节流数据
print(response.content)
print(response.content.decode('utf-8'))

with open("baidu.html", "w", encoding='utf-8') as fp:
    fp.write(response.content.decode('utf-8'))

# # 查看完整url地址
print(response.url)  # http://www.baidu.com/s?wd=%E6%94%BB%E5%9F%8E%E7%8C%B4
# # 查看响应头部字符编码
print(response.encoding)
# # 查看响应码
print(response.status_code)
